package com.zg.autoflow.web;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.autoflow.req.AutoflowMainAllReq;
import com.zg.autoflow.req.AutoflowSubAddReq;
import com.zg.autoflow.service.AutoflowSubService;
import com.zg.autoflow.vo.*;
import com.zg.common.core.R;
import com.zg.common.core.dao.BaseEntity;
import com.zg.common.core.dao.autoflow.dao.AutoflowStepSubDao;
import com.zg.common.core.dao.autoflow.dao.AutoflowStepSubPersonDao;
import com.zg.common.core.dao.autoflow.dao.AutoflowSubDao;
import com.zg.common.core.dao.autoflow.entity.AutoflowStepSub;
import com.zg.common.core.dao.autoflow.entity.AutoflowStepSubPerson;
import com.zg.common.core.dao.autoflow.entity.AutoflowSub;
import com.zg.common.core.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author zg
*/
@RestController
@RequiredArgsConstructor
@Api(tags = {"子流程配置"})
public class AutoflowSubController {
	private final AutoflowSubDao autoflowSubDao;
	private final AutoflowStepSubDao autoflowStepSubDao;
	private final AutoflowStepSubPersonDao autoflowStepSubPersonDao;
	private final AutoflowSubService autoflowSubService;

	@ApiOperation("新增子流程")
	@PostMapping("/autoFlow/sub/addOrUpdate")
	public R<?> addOrUpdate(@RequestBody @Valid AutoflowSubAddReq param) {
		AutoflowSub obj = autoflowSubService.addOrUpdate(param);
		return R.success(obj);
	}
	@ApiOperation("删除子流程")
	@PostMapping("/autoFlow/sub/del")
	public R<?> del(@RequestBody List<Long> idList) {
		autoflowSubService.del(idList);
		return R.success();
	}
	@ApiOperation("查询所有子流程")
	@PostMapping("/autoFlow/sub/all")
	public R<List<AutoflowSubVo>> all(@RequestBody @Valid AutoflowMainAllReq param) {
		if (ObjectUtils.isEmpty(param.getFlowCode())) BusinessException.happen("流程code不能为空");
		List<AutoflowSub> list = autoflowSubDao.list(new LambdaQueryWrapper<AutoflowSub>().eq(AutoflowSub::getFlowCode,param.getFlowCode())
				.eq(BaseEntity::getIsDel,false).orderByDesc(BaseEntity::getCreateTime));
		List<AutoflowSubVo> autoflowSubVos = new ArrayList<>();
		list.forEach(line->{
			AutoflowSubVo autoflowSubVo = new AutoflowSubVo();
			BeanUtil.copyProperties(line,autoflowSubVo);
			List<AutoflowStepSub> list1 = autoflowStepSubDao.list(new LambdaQueryWrapper<AutoflowStepSub>().eq(AutoflowStepSub::getSubFlowId, line.getId())
					.eq(BaseEntity::getIsDel, false).orderByAsc(AutoflowStepSub::getStepNo));
			List<AutoflowStepSubVo> autoflowStepSubVos = BeanUtil.copyToList(list1, AutoflowStepSubVo.class);
			List<AutoflowStepSubPerson> stepSubPersonList = autoflowStepSubPersonDao.list(new LambdaQueryWrapper<AutoflowStepSubPerson>()
					.eq(AutoflowStepSubPerson::getSubFlowId, line.getId()).eq(BaseEntity::getIsDel, false));
			Map<String, List<Long>> groupedNames = stepSubPersonList.stream().collect(Collectors.groupingBy(
					AutoflowStepSubPerson::getStepCode, Collectors.mapping(
							AutoflowStepSubPerson::getStepHandler,
							Collectors.toList()
					)));
			autoflowStepSubVos.forEach(autoflowStepSubVo -> {
				autoflowStepSubVo.setPersonList(groupedNames.get(autoflowStepSubVo.getStepNo()));
			});
			autoflowSubVo.setStepSubVos(autoflowStepSubVos);
			autoflowSubVos.add(autoflowSubVo);
		});
		return R.success(autoflowSubVos);
	}

}
